<?php

namespace app\common\model\trendsinfo;

use app\common\model\trends\Trends;
use app\common\model\User;
use think\Model;


class Trendsinfo extends Model
{


    // 表名
    protected $name = 'trends_user_like';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'create_time_text'
    ];


    public function getStatusList()
    {
//        return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4')];
        return ['1' => __('Status 1'), '3' => __('Status 3'), '4' => __('Status 4')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }



    const cache = 60;
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id')->bind(['nickname' => 'nickname'])->cache(self::cache);
    }

    public function trends()
    {
        return $this->hasOne(Trends::class, 'id', 'trends_id')->bind(['title' => 'id'])->cache(self::cache);
    }

}
